package cn.stylefeng.guns.modular.system.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.core.util.DateUtil2;
import cn.stylefeng.guns.modular.basic.model.params.BasicOperateParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicOperateResult;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.modular.task.model.params.TaskDetailedParam;
import cn.stylefeng.guns.modular.task.model.params.TaskOrderParam;
import cn.stylefeng.guns.modular.task.model.result.TaskDetailedResult;
import cn.stylefeng.guns.modular.task.model.result.TaskOrderResult;
import cn.stylefeng.guns.modular.task.service.TaskDetailedService;
import cn.stylefeng.guns.modular.task.service.TaskOrderService;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;

/**
 * 作业面版
 */
@Controller
@RequestMapping("/panel")
public class panelController extends BaseController{

    private String PREFIX = "/panel";

    @Autowired
    private UserService userService;
    
    @Autowired
    private TaskOrderService taskOrderService;

    @Autowired
    private TaskDetailedService taskDetailedService;

    /**
     * 跳转到作业面板
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/taskpanel.html";
    }
    
    /**
     * 查询作业 总量
     */
    @ResponseBody
    @RequestMapping("/findSum")
    public ResponseData findSum(TaskOrderParam taskOrderParam) {
    	TaskOrderResult r1 = new TaskOrderResult();
    	taskOrderParam.setDeliverId(userService.getDeliverId());
    	taskOrderParam.setCreateTimes(DateUtil.getDate().substring(0, 7));
    	TaskOrderResult taskOrderResult = taskOrderService.findSum(taskOrderParam);
    	if(taskOrderResult != null) {
    		r1.setHours(taskOrderResult.getHours());
    	}
    	
    	taskOrderParam.setSettlementMode("体积");
    	taskOrderResult = taskOrderService.findSum2(taskOrderParam);
    	if(taskOrderResult != null) {
    		BigDecimal prescription = new BigDecimal(taskOrderResult.getPrescription());
        	taskOrderResult = taskOrderService.findCount(taskOrderParam);
    		BigDecimal count = new BigDecimal(taskOrderResult.getV1());
    		BigDecimal vprescription = prescription.divide(count,2,BigDecimal.ROUND_HALF_DOWN);
    		r1.setVolume(vprescription.toString());
    	}
    	
    	taskOrderParam.setSettlementMode("数量");
    	taskOrderResult = taskOrderService.findSum2(taskOrderParam);
    	if(taskOrderResult != null) {
    		BigDecimal prescription = new BigDecimal(taskOrderResult.getPrescription());
        	taskOrderResult = taskOrderService.findCount(taskOrderParam);
    		BigDecimal count = new BigDecimal(taskOrderResult.getV1());
    		BigDecimal nprescription = prescription.divide(count,2,BigDecimal.ROUND_HALF_DOWN);
    		r1.setNums(nprescription.toString());
    	}
    	
    	taskOrderParam.setSettlementMode("重量");
    	taskOrderResult = taskOrderService.findSum2(taskOrderParam);
    	if(taskOrderResult != null) {
    		BigDecimal prescription = new BigDecimal(taskOrderResult.getPrescription());
        	taskOrderResult = taskOrderService.findCount(taskOrderParam);
    		BigDecimal count = new BigDecimal(taskOrderResult.getV1());
    		BigDecimal wprescription = prescription.divide(count,2,BigDecimal.ROUND_HALF_DOWN);
    		r1.setWeights(wprescription.toString());
    	}
    	
        return ResponseData.success(r1);
    }
    
    /**
     * 查询  项目月度平均时效排名
     */
    @ResponseBody
    @RequestMapping("/findshixiaopaiming")
    public ResponseData findshixiaopaiming(TaskOrderParam taskOrderParam) {
    	List<TaskOrderResult> reslist = new ArrayList<TaskOrderResult>();
    	
    	BasicOperateParam basicOperateParam = new BasicOperateParam();
    	basicOperateParam.setDeliverId(userService.getDeliverId());
    	List<BasicOperateResult> oarr = basicOperateService.findListBySpec(basicOperateParam,null);
    	
    	taskOrderParam.setDeliverId(userService.getDeliverId());
    	taskOrderParam.setCreateTimes(DateUtil.getDate().substring(0, 7));
    	
    	for (BasicOperateResult o : oarr) {
    		taskOrderParam.setOperateId(o.getId());
    		TaskOrderResult taskOrderResult = taskOrderService.findSum2(taskOrderParam);
    		if(taskOrderResult != null) {
    			BigDecimal prescription = new BigDecimal(taskOrderResult.getPrescription());
    			taskOrderResult = taskOrderService.findCount(taskOrderParam);
    			BigDecimal count = new BigDecimal(taskOrderResult.getV1());
    			BigDecimal vprescription = prescription.divide(count,2,BigDecimal.ROUND_HALF_DOWN);
    			
    			TaskOrderResult rr = new TaskOrderResult();
    			rr.setOperateName(o.getAbbreviation());
    			rr.setPrescription(vprescription.toString());
    			reslist.add(rr);
    		}
		}
    	Collections.reverse(reslist);
        return ResponseData.success(reslist);
    }
    
    /**
     * 查询项目日作业排名
     */
    @ResponseBody
    @RequestMapping("/panelOperateDay")
    public ResponseData panelOperateDay(TaskOrderParam taskOrderParam) {
    	taskOrderParam.setDeliverId(userService.getDeliverId());
    	taskOrderParam.setCreateTimes(DateUtil.getDate());
    	List<TaskOrderResult> list = taskOrderService.panelOperateDay(taskOrderParam);
    	for (TaskOrderResult r : list) {
			if (taskOrderParam.getSettlementMode().equals("工时")) {
				r.setNums(r.getHours());
			}
			if (taskOrderParam.getSettlementMode().equals("体积")) {
				r.setNums(r.getVolume());
			}
			if (taskOrderParam.getSettlementMode().equals("重量")) {
				r.setNums(r.getWeights());
			}
		}
        return ResponseData.success(list);
    }
    
    /**
     * 查询项目月作业排名
     */
    @ResponseBody
    @RequestMapping("/panelOperateMonth")
    public ResponseData panelOperateMonth(TaskOrderParam taskOrderParam) {
    	taskOrderParam.setDeliverId(userService.getDeliverId());
    	taskOrderParam.setCreateTimes(DateUtil.getDate().substring(0, 7));
    	List<TaskOrderResult> list = taskOrderService.panelOperateMonth(taskOrderParam);
    	for (TaskOrderResult r : list) {
			if (taskOrderParam.getSettlementMode().equals("工时")) {
				r.setNums(r.getHours());
			}
			if (taskOrderParam.getSettlementMode().equals("体积")) {
				r.setNums(r.getVolume());
			}
			if (taskOrderParam.getSettlementMode().equals("重量")) {
				r.setNums(r.getWeights());
			}
		}
        return ResponseData.success(list);
    }
    
    /**
     * 查询 项目月度作业对比
     */
    @ResponseBody
    @RequestMapping("/panelOperateMonthDuibi")
    public ResponseData panelOperateMonthDuibi(TaskOrderParam taskOrderParam) {
    	taskOrderParam.setDeliverId(userService.getDeliverId());
    	taskOrderParam.setCreateTimes(DateUtil.getDate().substring(0, 7));
    	List<TaskOrderResult> list = taskOrderService.panelOperateMonthDuibi(taskOrderParam);
    	for (TaskOrderResult r : list) {
    		taskOrderParam.setOperateId(r.getOperateId());
    		taskOrderParam.setCreateTimes(DateUtil2.getLastMonth());
    		TaskOrderResult taskOrderResult =  taskOrderService.findSum(taskOrderParam);
    		if(taskOrderResult != null) {
        		if(ToolUtil.isNotEmpty(taskOrderResult.getNums())) {
            		r.setV1(taskOrderResult.getNums());
        		} else {
            		r.setV1("0");
        		}
        		if (ToolUtil.isNotEmpty(taskOrderResult.getHours())) {
        			r.setV2(taskOrderResult.getHours());
    			} else {
        			r.setV2("0");
    			}
        		if(ToolUtil.isNotEmpty(taskOrderResult.getVolume())) {
        			r.setV3(taskOrderResult.getVolume());
        		} else {
        			r.setV3("0");
        		}
        		if (ToolUtil.isNotEmpty(taskOrderResult.getWeights())) {
        			r.setV4(taskOrderResult.getWeights());
    			} else {
        			r.setV4("0");
    			}
    		} else {
        		r.setV1("0");
        		r.setV2("0");
        		r.setV3("0");
        		r.setV4("0");
    		}
		}
    	if(list.size()>0) {
    		list.get(0).setV5(DateUtil.getDate().substring(5, 7));
    		list.get(0).setV6(DateUtil2.getLastMonth().substring(5, 7));
    	}
        return ResponseData.success(list);
    }
    
    /**
     * 查询工人月度作业排名
     */
    @ResponseBody
    @RequestMapping("/sumGongrenMonth")
    public ResponseData panelOperateMonth(TaskDetailedParam taskDetailedParam) {
    	taskDetailedParam.setDeliverId(userService.getDeliverId());
    	taskDetailedParam.setStartTime(DateUtil.getDate().substring(0, 7));
    	List<TaskDetailedResult> list = taskDetailedService.sumGongrenMonth(taskDetailedParam);
    	for (TaskDetailedResult r : list) {
			if (taskDetailedParam.getSettlementMode().equals("工时")) {
				r.setNums(r.getHours());
			}
			if (taskDetailedParam.getSettlementMode().equals("体积")) {
				r.setNums(r.getVolume());
			}
			if (taskDetailedParam.getSettlementMode().equals("重量")) {
				r.setNums(r.getWeights());
			}
		}
        return ResponseData.success(list);
    }
    
    /**
     * 查询 项目月度作业类型总量
     */
    @ResponseBody
    @RequestMapping("/findSumtaskWork")
    public ResponseData findSumtaskWork(TaskOrderParam taskOrderParam) {
    	taskOrderParam.setDeliverId(userService.getDeliverId());
    	taskOrderParam.setCreateTimes(DateUtil.getDate().substring(0, 7));
    	List<TaskOrderResult> list = taskOrderService.findSumtaskWork(taskOrderParam);
        return ResponseData.success(list);
    }

    @Autowired
    private BasicOperateService basicOperateService;

    /**
     * 查询 各项目月度作业人数
     */
    @ResponseBody
    @RequestMapping("/sumPeoples")
    public ResponseData sumPeoples(TaskDetailedParam taskDetailedParam) {
    	BasicOperateParam basicOperateParam = new BasicOperateParam();
    	basicOperateParam.setDeliverId(userService.getDeliverId());
    	List<BasicOperateResult> oarr = basicOperateService.findListBySpec(basicOperateParam,null);
    	
    	List<TaskOrderParam> reslist = new ArrayList<TaskOrderParam>();
    	for (BasicOperateResult o : oarr) {
        	taskDetailedParam.setStartTime(DateUtil.getDate().substring(0, 7));
        	TaskDetailedResult r = taskDetailedService.sumPeoples(taskDetailedParam,o.getId());
        	int a = Integer.parseInt(r.getNums());
        	if(a>0) {
        		TaskOrderParam TaskOrderParam = new TaskOrderParam();
        		TaskOrderParam.setOperateName(o.getAbbreviation());
        		TaskOrderParam.setPeoples(a+"");
        		reslist.add(TaskOrderParam);
        	}
        	
		}
        return ResponseData.success(reslist);
    }

    /**
     * 查询 流动作业数据
     */
    @ResponseBody
    @RequestMapping("/findlimit9task")
    public ResponseData findlimit9task(TaskDetailedParam taskDetailedParam) {
    	taskDetailedParam.setDeliverId(userService.getDeliverId());
    	List<TaskDetailedResult> list = taskDetailedService.findlimit9task(taskDetailedParam);
        return ResponseData.success(list);
    }
    
    /**
     * 跳转到TMS面板
     */
    @RequestMapping("tmspanel")
    public String tmspanel() {
//        return PREFIX + "/tmspanel.html";
    	return PREFIX + "/tmspanel_fc.html";//先给个不存在的页面进行封存，此功能查询时间超过30分钟
    }

    /**
     * 跳转到TMS面板2
     */
    @RequestMapping("tmspanel2")
    public String tmspanel2() {
//        return PREFIX + "/tmspanel2.html";
        return PREFIX + "/tmspanel2_fc.html";//先给个不存在的页面进行封存，此功能查询时间超过30分钟
    }

    /**
     * 跳转到WMS面板
     */
    @RequestMapping("wmspanel")
    public String wmspanel() {
        return PREFIX + "/wmspanel.html";
    }

    /**
     * 跳转到w6TMS数据分析
     */
    @RequestMapping("w6tmspanel")
    public String w6tmspanel() {
        return PREFIX + "/w6tms.html";
    }

    /**
     * 跳转到w6WMS数据分析
     */
    @RequestMapping("w6wmspanel")
    public String w6wmspanel() {
        return PREFIX + "/w6wms.html";
    }
    
    /**
     * 查询w6tms接口
     */
    @ResponseBody
    @RequestMapping("/w6tms")
    public ResponseData w6tms(String month) {
    	String jsonStr = DateUtil.interfaceUtil("http://39.105.212.172:8086/website/ZS_TMS_DATA_INTERFACE.aspx?month="+month, "");
        return ResponseData.success(jsonStr);
    }
    
    /**
     * 查询w6wms接口
     */
    @ResponseBody
    @RequestMapping("/w6wms")
    public ResponseData w6wms(String kfmc,String month) {
    	String jsonStr = DateUtil.interfaceUtil("http://39.105.212.172:8086/website/ZS_WMS_DATA_INTERFACE.aspx?KFMC="+kfmc+"&month="+month, "");
        return ResponseData.success(jsonStr);
    }

    /**
     * 跳转到 w6月度运输指标统计
     */
    @RequestMapping("yueduyunshuZhibiaotongji")
    public String yueduyunshuZhibiaotongji() {
        return PREFIX + "/yueduyunshuZhibiaotongji.html";
    }

    /**
     * 跳转到 w6月度运输线路指标统计
     */
    @RequestMapping("yueduyunshuxianluzhgibiaoTongji")
    public String yueduyunshuxianluzhgibiaoTongji() {
        return PREFIX + "/yueduyunshuxianluzhgibiaoTongji.html";
    }

    /**
     * 跳转到 w6月度运输线路
     * 价格差异统计
     */
    @RequestMapping("yueduyunshuxianluzhgibiaoJiaGe")
    public String yueduyunshuxianluzhgibiaoJiaGe() {
        return PREFIX + "/yueduyunshuxianluzhgibiaoJiaGe.html";
    }

}
